/*
 * Copyright (C) 2012, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view;

import xowl.gmi.environment.Locale;

/**
 *
 * @author Laurent WOUTERS
 */
public class DialogOnException extends javax.swing.JDialog {
    private static int width = 500;
    private static int height = 350;
    
    public static interface Actions {
        void reportEmail(boolean config, boolean workbench);
        void reportOnline(boolean config, boolean workbench);
    }
    
    private Actions actions;
    
    public DialogOnException(Actions actions, Locale locale, javax.swing.JFrame frame, String error) {
        super(frame);
        this.actions = actions;
        initComponents(locale, frame, error);
    }
    
    private void initComponents(Locale locale, javax.swing.JFrame mainFrame, String error) {
        java.awt.Rectangle bounds = mainFrame.getBounds();
        int x = bounds.x + (bounds.width-width)/2;
        int y = bounds.y + (bounds.height-height)/2;
        this.setBounds(x, y, width, height);
        this.setModalityType(javax.swing.JDialog.ModalityType.APPLICATION_MODAL);
        this.setResizable(false);
        this.setTitle(locale.expDlgTitle);
        this.setIconImage(IconProvider.getEmbedded("bug.png").getImage());
        
        javax.swing.JLabel header = new javax.swing.JLabel("<html>"+locale.expDlgHeader+"</html>");
        javax.swing.JTextArea exception = new javax.swing.JTextArea(error);
        javax.swing.JLabel report = new javax.swing.JLabel("<html>"+locale.expDlgReport+"</html>");
        final javax.swing.JCheckBox checkConfig = new javax.swing.JCheckBox(locale.expDlgReportSystem);
        final javax.swing.JCheckBox checkWorkbench = new javax.swing.JCheckBox(locale.expDlgReportWorkbench);
        javax.swing.JButton email = new javax.swing.JButton(locale.expDlgReportEMail, IconProvider.getEmbedded("bug.png"));
        javax.swing.JButton online = new javax.swing.JButton(locale.expDlgReportOnline, IconProvider.getEmbedded("bug.png"));
        javax.swing.JLabel advice = new javax.swing.JLabel("<html>"+locale.expDlgAdvice+"</html>");
        javax.swing.JButton close = new javax.swing.JButton(locale.expDlgClose);
        exception.setFont(new java.awt.Font("Sans-serif", java.awt.Font.PLAIN, 10));
        javax.swing.JScrollPane scroll = new javax.swing.JScrollPane(exception);
        
        email.addActionListener(new javax.swing.AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent ae) {
                actions.reportEmail(checkConfig.isSelected(), checkWorkbench.isSelected());
            }
        });
        online.addActionListener(new javax.swing.AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent ae) {
                actions.reportOnline(checkConfig.isSelected(), checkWorkbench.isSelected());
            }
        });
        close.addActionListener(new javax.swing.AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent ae) { setVisible(false); }
        });
        
        javax.swing.JPanel content = (javax.swing.JPanel)getContentPane();
        content.setLayout(new javax.swing.BoxLayout(content, javax.swing.BoxLayout.Y_AXIS));
        content.add(javax.swing.Box.createVerticalStrut(10));
        content.add(getRow(header));
        content.add(getRow(scroll));
        content.add(javax.swing.Box.createVerticalStrut(10));
        content.add(getRow(report));
        content.add(getRow(checkConfig));
        content.add(getRow(checkWorkbench));
        content.add(getRow(email, online));
        content.add(javax.swing.Box.createVerticalStrut(10));
        content.add(getRow(advice));
        content.add(getRow(close));
        content.add(javax.swing.Box.createVerticalStrut(10));
    }
    
    private javax.swing.JPanel getRow(javax.swing.JComponent comp) {
        comp.setMaximumSize(new java.awt.Dimension(width-20, Integer.MAX_VALUE));
        javax.swing.JPanel row = new javax.swing.JPanel();
        row.setLayout(new javax.swing.BoxLayout(row, javax.swing.BoxLayout.X_AXIS));
        row.add(javax.swing.Box.createHorizontalStrut(10));
        row.add(comp);
        row.add(javax.swing.Box.createHorizontalStrut(10));
        row.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
        return row;
    }
    private javax.swing.JPanel getRow(javax.swing.JComponent comp1, javax.swing.JComponent comp2) {
        javax.swing.JPanel row = new javax.swing.JPanel();
        row.setLayout(new javax.swing.BoxLayout(row, javax.swing.BoxLayout.X_AXIS));
        row.add(javax.swing.Box.createHorizontalStrut(10));
        row.add(comp1);
        row.add(javax.swing.Box.createHorizontalGlue());
        row.add(comp2);
        row.add(javax.swing.Box.createHorizontalStrut(10));
        row.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
        return row;
    }
}
